package com.chennan.redis.domain.service.singleflight;

import ch.qos.logback.core.encoder.ByteArrayUtil;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author chennan
 * @createTime 2021年06月29日 11:33:00
 */
public class SingleFlightTest {
    public static void main(String[] args) {
        CallManage callManage = new CallManage();
        int count = 10;
        CountDownLatch cld = new CountDownLatch(count);
        ExecutorService executorService = Executors.newFixedThreadPool(count);
        for (int i = 0; i < count; i++) {
            executorService.execute(() -> {
                try {
                    cld.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                byte[] value = callManage.run("key", () -> {
                    System.out.println("func");
                    try {
                        TimeUnit.SECONDS.sleep(2);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    return ByteArrayUtil.hexStringToByteArray("bar");
                });
                System.out.println(ByteArrayUtil.toHexString(value).toString());
            });
            cld.countDown();
        }
        executorService.shutdown();
//            new Thread(()->{
//                int i1 = (int) (Math.random() * 10);
//                try {
//                        TimeUnit.SECONDS.sleep(i1);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                System.out.println(Thread.currentThread().getName()+"等待"+i1 +"完毕运行");
//                cld.countDown();
//            }).start();
//
//        }
//        try {
//            System.out.println("等待中。。。。。");
//            cld.await();
//            System.out.println("等待完毕、。。。。。");
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
    }
}
